package com.sinodata.ns.cell;

import jt.classic.system.SimpleManager;
import jt.classic.system.group.GroupManager;
import jt.classic.system.group.IGroup;

import org.limp.mine.DataTrimmerI.DataItemList;

import java.sql.Connection;

import java.util.List;
import java.util.Map;


/**
 * 监理标段管理者
 * 
 * @author mido
 *
 */
public class JLBDManager extends SimpleManager {
    public JLBDManager(Connection conn) {
        super(conn);
    }

    /**
     * 获取监理标段信息
     * @param id 主键ID
     * @return JLBDInfo
     */
    public JLBDInfo getJLBDInfoById(String id) {
        String sql = "SELECT * FROM APP_NS_SVBD WHERE ID = ?";
        DataItemList dil = this.trimmerI.createDIL();
        dil.set(id);

        Map map = this.trimmerI.searchSingleData(sql, dil);

        JLBDInfo info = null;

        if (map != null) {
            info = new JLBDInfo();

            //选择组
            String groupId = (String) map.get("SVBD_GROUPID");

            GroupManager gm = new GroupManager(this.conn);

            IGroup group = gm.getGroupById(groupId);

            map.put("GROUP", group);
            
            map.put("GROUPID", group != null ? group.id() : "");
            
            map.put("GROUPNAME", group != null ? group.iname() : "'");

            sql = "SELECT * FROM APP_NS_CELL, (SELECT APP_NS_CELLBD.C_ID FROM APP_NS_SVBDITEM,APP_NS_CELLBD WHERE " +
                "APP_NS_SVBDITEM.CB_ID = APP_NS_CELLBD.CB_ID AND APP_NS_SVBDITEM.ID = ? GROUP BY APP_NS_CELLBD.C_ID) A WHERE APP_NS_CELL.C_ID = A.C_ID";

            Map cellMap = this.trimmerI.searchSingleData(sql, dil);

            if (cellMap != null) {
                CellInfo cellInfo = new CellInfo();

                sql = "SELECT APP_NS_CELLBD.* FROM APP_NS_SVBDITEM,APP_NS_CELLBD WHERE " +
                    "APP_NS_SVBDITEM.CB_ID = APP_NS_CELLBD.CB_ID AND APP_NS_SVBDITEM.ID = ?";

                List list = this.trimmerI.searchMultiData(sql, dil);

                for (int i = 0; i < list.size(); i++) {
                    Map temp = (Map) list.get(i);

                    CellBDInfo cbinfo = new CellBDInfo(cellInfo);

                    cbinfo.reload(temp);

                    list.set(i, cbinfo);
                }

                cellMap.put("CELLBD", list);

                cellInfo.reload(cellMap);

                map.put("CELLINFO", cellInfo);

                info.reload(map);
            }
        }

        return info;
    }

    /**
     * 获取设计单元关联的监理标段信息
     * @param cellid 设计单元ID
     * @return List
     */
    public List getJLBDInfoByCellId(String cellid) {
        String sql =
            "SELECT APP_NS_SVBD.ID FROM APP_NS_SVBD, (SELECT APP_NS_SVBDITEM.ID FROM APP_NS_SVBDITEM, " +
            "APP_NS_CELLBD WHERE APP_NS_SVBDITEM.CB_ID = APP_NS_CELLBD.CB_ID AND APP_NS_CELLBD.C_ID = ? " +
            "GROUP BY APP_NS_SVBDITEM.ID) A WHERE APP_NS_SVBD.ID = A.ID";
        DataItemList dil = this.trimmerI.createDIL();
        dil.set(cellid);

        List list = this.trimmerI.searchMultiData(sql, dil);

        if ((list != null) && !list.isEmpty()) {
            for (int i = 0; i < list.size(); i++) {
                Map temp = (Map) list.get(i);

                String id = (String) temp.get("ID");

                JLBDInfo jd = this.getJLBDInfoById(id);
                
                list.set(i, jd);
            }
        }

        return list;
    }
}
